package cn.iocoder.yudao.module.ai.service.datasetreleasefile;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.datasetreleasefile.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.datasetreleasefile.DatasetReleaseFileDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.datasetreleasefile.DatasetReleaseFileMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 数据集解压缩文件列 Service 实现类
 *
 * @author 侯俊青
 */
@Service
@Validated
public class DatasetReleaseFileServiceImpl implements DatasetReleaseFileService {

    @Resource
    private DatasetReleaseFileMapper datasetReleaseFileMapper;

    @Override
    public Long createDatasetReleaseFile(DatasetReleaseFileSaveReqVO createReqVO) {
        // 插入
        DatasetReleaseFileDO datasetReleaseFile = BeanUtils.toBean(createReqVO, DatasetReleaseFileDO.class);
        datasetReleaseFileMapper.insert(datasetReleaseFile);
        // 返回
        return datasetReleaseFile.getId();
    }

    @Override
    public void updateDatasetReleaseFile(DatasetReleaseFileSaveReqVO updateReqVO) {
        // 校验存在
        validateDatasetReleaseFileExists(updateReqVO.getId());
        // 更新
        DatasetReleaseFileDO updateObj = BeanUtils.toBean(updateReqVO, DatasetReleaseFileDO.class);
        datasetReleaseFileMapper.updateById(updateObj);
    }

    @Override
    public void deleteDatasetReleaseFile(Long id) {
        // 校验存在
        validateDatasetReleaseFileExists(id);
        // 删除
        datasetReleaseFileMapper.deleteById(id);
    }

    private void validateDatasetReleaseFileExists(Long id) {
        if (datasetReleaseFileMapper.selectById(id) == null) {
            throw exception(DATASET_RELEASE_FILE_NOT_EXISTS);
        }
    }

    @Override
    public DatasetReleaseFileDO getDatasetReleaseFile(Long id) {
        return datasetReleaseFileMapper.selectById(id);
    }

    @Override
    public PageResult<DatasetReleaseFileDO> getDatasetReleaseFilePage(DatasetReleaseFilePageReqVO pageReqVO) {
        return datasetReleaseFileMapper.selectPage(pageReqVO);
    }

    @Override
    public void deleteByDatasetId(Long datasetId) {
        datasetReleaseFileMapper.delete(DatasetReleaseFileDO::getDatasetId, datasetId);
    }

    @Override
    public List<DatasetReleaseFileRespVO> getOriginalFileListByDatasetId(Long datasetId) {
        LambdaQueryWrapper<DatasetReleaseFileDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DatasetReleaseFileDO::getDatasetId, datasetId);
        queryWrapper.eq(DatasetReleaseFileDO::getFileBizType, 1);
        queryWrapper.orderByDesc(DatasetReleaseFileDO::getSortNum);
        List<DatasetReleaseFileDO> datasetReleaseFileDOS = datasetReleaseFileMapper.selectList(queryWrapper);
        return BeanUtils.toBean(datasetReleaseFileDOS, DatasetReleaseFileRespVO.class);
    }
}